lower_case <- c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z")
upper_case <- c("A", "B", "C", "D", "E", "F", "G", "H" "I", "J", "K", "L", "M",
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z")
punctuation <- c(".", ",", "!", "?", "'", """, "(", ")", " ", "-", ";", ":")Practice Activity 1: Find the Mistakes
Today you will be creating and manipulating vectors, lists, and data frames to uncover a top secret message.
Some advice:
Work with those around you
Google is your friend! If an error is confusing, copy it into Google and see what other people are saying. If you don’t know how to do something, search for it.
Just because there is no error message doesn’t mean everything went smoothly. Use the console to check each step and make sure you have accomplished what you wanted to accomplish.
Part One: Setup
Each of the following R chunks will cause an error and / or do the desired task incorrectly. Find the mistake, and correct it to complete the intended action.
- Create vectors containing the upper case letters, lower case letters, and some punctuation marks.
- Make one long vector containing all the symbols.
my_symbols <- cbind(lower_case, upper_case, punctuation)- Turn the
my_symbolsvector into a data frame, with one column named “Symbol”.
my_symbols <- dataframe(Symbol = my_symbols)- Find the total number of symbols we have in our data frame.
len <- length(my_symbols)- Create a new variable in your dataframe that assigns a number to each symbol.
my_symbols%Num <- 1:lenPart Two: Decoding the secret message.
This chunk will load up the encoded secret message as a vector:
top_secret <- read_csv("https://www.dropbox.com/s/k72h1zewk4gtqep/PA_Secret_Code?dl=1",
col_names = FALSE)$X1By altering this top secret set of numbers, you will be able to create a message. Write your own code to complete the steps, in the order given below.
Add 14 to every number.
Multiply every number by 18, then subtract 257.
Use the
exp()function to exponentiate every number.Square every number.
Hint: To update a vector after performing an operation, you overwrite the existing object with its updated counterpart. This looks something like this:
x <- x + 12,
where the original value(s) in x have had 12 added to them, and the resulting values are put back in to the object named x.
## Code to carry out steps 6-9 Checkpoint: Headquarters has informed you that at this stage of decoding, there should be 352 numbers in the secret message that are below 17. Write the code to verify that this is true for your top_secret object!
Hint: This is what is called a “relational” comparison, where you compare an object to a number and R will give you a vector of TRUEs and FALSEs based on whether the comparison is / is not met. You can then use these TRUEs and FALSEs as numbers, since TRUE = 1 and FALSE = 0 in R land.
# Write code to verify that there are 352 numbers with values **below** 17Next, carry out the following steps:
Turn your vector of numbers into a matrix with 5 columns.
Separately from your top secret numbers, create a vector of all the even numbers between 1 and 382. Name it “evens”. That is, “evens” should contain 2, 4, 6, 8 …, 382.
Subtract the “evens” vector from the first column of your secret message matrix.
Subtract 100 from all numbers 18-24th rows of the 3rd column.
Multiply all numbers in the 4th and 5th column by 2.
Turn your matrix back into a vector.
## Code to carry out steps 10-15Checkpoint: Headquarters has informed you that at this stage of decoding, all numbers in indices 500 and beyond are below 100. Write the code to verify that this is true for your top_secret object!
Hint: Use a relational comparison similar to what you used in the last checkpoint, but here you will need to subset values from your vector!
# Write code to verify that indices 500 and beyond have values **below** 100Take the square root of all numbers in indices 38 to 465.
Use the
round()function to round all numbers to the nearest whole number.Replace all instances of the number 39 with 20.
Hint: Step 18 requires another relational comparison, but this time it is equality. Equality in R is checked with a double equal sign rather than a single equal sign!
# Code for steps 16-18Checkpoint: Headquarters has informed you that your final message should have 344 even numbers.
Hint: Checking for divisibility is an interesting operation that isn’t done much in R. Modulus is the operation you are interested in, where you are checking for whether the numbers are divisible by 2, with no remainder. See what you can find about modulus in R!
# Code to verify how many even numbers are in your top_secret vectorPart 3: The secret message!
Use your final vector of numbers as indices for my_symbols to discover the final message, by running the following code:
stringr::str_c(my_symbols$Symbol[top_secret], collapse = "")Google the first line of this message, if you do not recognize it, to see what poem it is.